{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 指数(幂运算)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 简介"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "指数，又称幂运算，表达式为 $a^{n}$ ，读作“$a$ 的 $n$ 次方”或“$a$ 的 $n$ 次幂”。其中，$a$ 称为底数，$n$ 称为指数，通常指数写成上标，当不能用上标时，例如在 Python 和 JS 中，通常写成 `a ** n` ，其他地方也有写做 `a^n`；也可视为超运算，记为`a[3]n`；亦可以用高德纳箭号表示法，写成  $a\\uparrow n$。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 运算法则"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 同底数幂相乘，底数不变，指数相加：\n",
    "\n",
    "$a^{m}\\times a^{n} = a^{m+n}$\n",
    "\n",
    "* 同底数幂相除，底数不变，指数相减：\n",
    "\n",
    "$\\frac{a^{m}} {a^{n}} = a^{m-n}$\n",
    "\n",
    "* 同指数幂相除，指数不变，底数相除：\n",
    "\n",
    "${\\frac {a^{n}}{b^{n}}} = (\\frac{a}{b})^n$\n",
    "\n",
    "* 分数次幂\n",
    "\n",
    "$x^{\\frac{m}{n}} = (x^m)^{\\frac{1}{n}} = \\sqrt[n]{x^m}$\n",
    "\n",
    "* 负数次幂\n",
    "\n",
    "$x^{-m} = \\frac{1}{x^m} (x \\neq 0)$ \n",
    "\n",
    "* 非 0 的 0 次幂\n",
    "\n",
    "$x^0 = 1 (x \\neq 0)$\n",
    "\n",
    "* 任意数的 1 次幂等于其本身\n",
    "\n",
    "$x^1 = x$\n",
    "\n",
    "* 非 0 的负一次幂等于其倒数\n",
    "\n",
    "$x^{-1} = \\frac {1}{x} (x \\neq 0)$\n",
    "\n",
    "指数运算不满足交换率和结合律"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "0 的 0 次幂数学上还没有严格定义，但在 Python 和 JS 里，都实现为 0 ** 0 = 1，遵循 [IEEE754 标准](https://zh.wikipedia.org/wiki/IEEE_754)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 指数的极限"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* $当 a > 1, n \\to \\infty，a^n \\to \\infty$\n",
    "* $当 a < -1, n \\to -\\infty 或 a^n \\to \\infty (视 n 奇偶性而定)$\n",
    "* (-1, 1)间的数的幂趋于0: $当 |a| < 1, n \\to \\infty, a^n \\to 0$\n",
    "* 1 的幂永远都是 1： $当 a = 1, n \\to \\infty, a^n \\to 1$\n",
    "* -1 的幂不具有收敛性\n",
    "* 如果 a 左趋近于 1，而它的幂趋于无穷，注意这时候它的极限不是上面几个，而是[自然常数 $e$](https://math.haozi.me/E.html)\n",
    "\n",
    "${\\displaystyle \\lim _{n\\to \\infty }\\left(1+{\\frac {1}{n}}\\right)^{n}} = e$\n",
    "* 如果 a 右趋近于 1，则它的极限是自然常数 $e$ 的倒数\n",
    "\n",
    "${\\displaystyle \\lim _{n\\to \\infty }\\left(1-{\\frac {1}{n}}\\right)^{n}} = \\frac{1}{e}$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python 执行结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle 1$"
      ],
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sympy import *\n",
    "n = symbols('n')\n",
    "\n",
    "limit(1 ** n, n, oo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle \\infty$"
      ],
      "text/plain": [
       "oo"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit(1.1 ** n, n, oo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle e$"
      ],
      "text/plain": [
       "E"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit((1 + 1/n) ** n, n, oo)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle e^{-1}$"
      ],
      "text/plain": [
       "exp(-1)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "limit((1 - 1/n) ** n, n, oo)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## JS 实现"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里先暂时只讲一下当 n 为自然数下的计算方法，至于分数幂和负数幂，先按下不表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```javascript\n",
    "const pow = (a, n) => {\n",
    "  if (!Number.isInteger(n) || n < 0) throw Error\n",
    "\n",
    "  if (n === 0) return 1\n",
    "\n",
    "  let ret = a\n",
    "  for (let i = 1; i <n; ++i) {\n",
    "    ret *= a\n",
    "  }\n",
    "  return ret\n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "更有意思的问题，至于乘法怎么实现，加法怎么实现，再往下推要到二进制和处理器实现，由于 JS 暂不支持运算符重载，也不是本 repo 的重点，不再补充"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 其他"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "复数的幂，以及复数次幂，先不展开，以后再追加"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
